home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / fsovl / zipcomp.c < prev   
C/C++ Source or Header  |  1997-09-09  |  2KB  |  132 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  * ZIPCOMP.C - ZIP COMPRESSION
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. #define BITS  14    /* compression size */
  14.  
  15. Prototype int Compress(BPTR fh, char *buf, long bytes);
  16. Prototype void DeCompress(BPTR fh, char *buf, long bytes);
  17.  
  18. static long ErrorCode;
  19. static long Fh;
  20. static ubyte *MemBuf;
  21. static long  MemBytes;
  22.  
  23. static ubyte Buf[4096];
  24. static long BufIdx;
  25. static long BufLen;
  26.  
  27.  
  28. /*    WritePacket(fh, buf, bytes); */
  29. /*    ReadPacket(fh, buf, bytes);  */
  30.  
  31. int 
  32. Compress(BPTR fh, char *buf, long bytes)
  33. {
  34.     int r;
  35.  
  36.     MemBytes = bytes;
  37.     MemBuf = buf;
  38.     BufIdx = 0;
  39.     BufLen = 0;
  40.     ErrorCode = 0;
  41.     Fh = fh;
  42.     r = ZipCompress();
  43.     WritePacket(fh, Buf, BufIdx);
  44.     return(r);
  45. }
  46.  
  47. unsigned
  48. zfwrite(void *buf, long blkSize, long numBlks, void *fo)
  49. {
  50.     long n = blkSize * numBlks;
  51.  
  52.     while (n) {
  53.     long r;
  54.  
  55.     if ((r = sizeof(Buf) - BufIdx) == 0) {
  56.         WritePacket(Fh, Buf, sizeof(Buf));
  57.         BufIdx = 0;
  58.         r = sizeof(Buf);
  59.     }
  60.     if (n < r)
  61.         r = n;
  62.     movmem(buf, Buf + BufIdx, r);
  63.     buf = (void *)((char *)buf + r);
  64.     n -= r;
  65.     BufIdx += r;
  66.     }
  67.     return(numBlks);
  68. }
  69.  
  70. long
  71. zfread(char *buf, long bytes)
  72. {
  73.     long n = (bytes > MemBytes) ? MemBytes : bytes;
  74.  
  75.     movmem(MemBuf, buf, n);
  76.     MemBytes -= n;
  77.     MemBuf += n;
  78.     return(n);
  79. }
  80.  
  81. void 
  82. DeCompress(BPTR fh, char *buf, long bytes)
  83. {
  84.     MemBytes = bytes;
  85.     MemBuf = buf;
  86.     Fh = fh;
  87.     BufIdx = 0;
  88.     BufLen = 0;
  89.     ErrorCode = 0;
  90.     ZipDeCompress(MemBuf, MemBytes);
  91. }
  92.  
  93. int
  94. ReadByte(short *v)
  95. {
  96.     if (BufIdx == BufLen) {
  97.     BufIdx = 0;
  98.     BufLen = ReadPacket(Fh, Buf, sizeof(Buf));
  99.     if (BufLen == 0)
  100.         return(0);
  101.     }
  102.     *v = Buf[BufIdx++];
  103.     return(8);
  104. }
  105.  
  106. long
  107. readbuf(ubyte *buf, long n)
  108. {
  109.     long ttl = 0;
  110.  
  111.     while (n) {
  112.     long r = BufLen - BufIdx;
  113.  
  114.     if (r == 0) {
  115.         BufIdx = 0;
  116.         BufLen = ReadPacket(Fh, Buf, sizeof(Buf));
  117.         if (BufLen == 0)
  118.         break;
  119.         r = BufLen;
  120.     }
  121.     if (n < r)
  122.         r = n;
  123.     movmem(Buf + BufIdx, buf, r);
  124.     buf += r;
  125.     n -= r;
  126.     BufIdx += r;
  127.     ttl += r;
  128.     }
  129.     return(ttl);
  130. }
  131.  
  132.